import { showLoading, hideLoading } from 'react-redux-loading-bar';
import { queryUserList } from '../services/userService';
import { PAGE_SIZE } from '../constants/constants.js';

export default {
  namespace: 'user',
  state: {
    pageNum: 1,
    pageSize: PAGE_SIZE,
    total: 0,
    list: [],
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
      return history.listen(({ pathname }) => {
        dispatch({
          type: 'queryUserList',
          payload: {
            pageNum: 1,
            pageSize: PAGE_SIZE,
          },
        });
      });
    },
  },

  effects: {
    *queryUserList({ payload, }, { call, put, select }) { // eslint-disable-line
      try {
        yield put(showLoading());// 开始顶部进度条
        const response = yield call(queryUserList, payload);
        if (response.code === 200) {
          yield put({
            type: 'save',
            payload: {
              list: response.data.content,
              total: response.data.totalElements,
              pageNum: response.data.number,
              pageSize: payload.pageSize,
            },
          });
        }
      } finally {
        yield put(hideLoading());// 结束顶部进度条
      }
    },
  },
  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },

};
